package com.weixin.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.weixin.dao.User_Role_TbDAO;
import com.weixin.dao.impl.User_Role_TbDAOImpl;
import com.weixin.entity.Role_Tb;

import com.weixin.entity.User_Role_Tb;
import com.weixin.entity.User_Tb;
import com.weixin.service.Role_TbService;
import com.weixin.service.User_TbService;
import com.weixin.service.impl.Role_TbServiceImpl;
import com.weixin.service.impl.User_tbServiceImpl;

/**
 * Servlet implementation class User_TbServlet
 */
@WebServlet("/User_TbServlet/*")
public class User_TbServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	User_TbService us = new User_tbServiceImpl();
	Role_TbService rs = new Role_TbServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String sub = request.getPathInfo().substring(1);
		if ("login".equals(sub)) {
			login(request, response);
		} else if ("selectById".equals(sub)) {
			selectById(request, response);
		} else if ("Selectlist".equals(sub)) {
			Selectlist(request, response);
		} else if ("deleteById".equals(sub)) {
			deleteById(request, response);
		} else if ("deleteAll".equals(sub)) {
			deleteAll(request, response);
		} else if ("add".equals(sub)) {
			add(request, response);
		} else if ("preAdd".equals(sub)) {
			preAdd(request, response);
		} else if ("preAbb".equals(sub)) {
			preAbb(request, response);
		} else if ("listAllByRolePagination".equals(sub)) {
			listAllByRolePagination(request, response);
		} else if ("listAllByRoleSearchPagination".equals(sub)) {
			listAllByRoleSearchPagination(request, response);
		} else if ("preedit".equals(sub)) {
			preedit(request, response);
		} else if ("edit".equals(sub)) {
			edit(request, response);
		}else if("xiugai".equals(sub)) {
			xiugai(request, response);
		}else if("yz".equals(sub)) {
			yz(request, response);
		}else if("pwdnew".equals(sub)) {
			pwdnew(request, response);
		}else if("listAll".equals(sub)) {
			listAll(request, response);
		}

	}

	

	private void listAll(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String parameter = request.getParameter("admin");
		try {
			List<User_Tb> user_tb = us.getAll();
			request.getSession().setAttribute("user_tb", user_tb);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	private void pwdnew(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String name = request.getParameter("user_name");
		String password = request.getParameter("password");
		User_Tb ut=new User_Tb();
		ut.setUser_name(name);
		ut.setPassword(password);
		try {
			if (us.pwdnew(ut)) {
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}else {
				response.getWriter().print(2);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	private void yz(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String user_name = request.getParameter("user_name");
		System.out.println("======="+user_name);
		String question1 = request.getParameter("question1");
		System.out.println(question1);
		String answer1= request.getParameter("answer1");
		System.out.println(answer1);
		String question2 = request.getParameter("question2");
		System.out.println(question2);
		String answer2 = request.getParameter("answer2");
		System.out.println(answer2);
		User_Tb utb=null;
		try {
			if ((utb=us.yz(question1, answer1, question2, answer2, user_name))!=null) {
				request.getSession().setAttribute("utb", utb);
				response.sendRedirect(request.getContextPath() + "/page/wjmm/pwdnew.jsp");
			}else {
				request.setAttribute("err", "验证失败");
				request.getRequestDispatcher("/password.jsp").forward(request, response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	private void xiugai(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String name = request.getParameter("user_name");
		System.out.println(name);
		User_Tb ut=null;
		try {
			if ((ut=us.xiugai(name)) != null) {
				request.getSession().setAttribute("ut", ut);
				response.sendRedirect(request.getContextPath() + "/page/wjmm/pwdyz.jsp");
			}else {
				request.setAttribute("err", "验证失败");
				request.getRequestDispatcher("/page/wjmm/password.jsp").forward(request, response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	private void edit(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("user_id");
		System.out.println(id);
		String name = request.getParameter("user_name");
		System.out.println("============" + name);
		String pwd = request.getParameter("password");
		System.out.println("========="+pwd);
		String email = request.getParameter("email");
		String telephone = request.getParameter("telephone");
		String brithday = request.getParameter("brithday");
		System.out.println(brithday);
		String gender = request.getParameter("gender");
		String address = request.getParameter("address");
		String remark = request.getParameter("remark");
		User_Tb ut = new User_Tb();
		ut.setUser_id(Integer.valueOf(id));
		ut.setUser_name(name);
		ut.setPassword(pwd);
		ut.setEmail(email);
		ut.setTelephone(telephone);
		try {
			ut.setBrithday(new SimpleDateFormat("yyyy-MM-dd").parse(brithday));
			ut.setGender(gender);
			ut.setAddress(address);
			ut.setRemark(remark);
			if (us.updateSelect(ut)) {
				response.getWriter().println(1);
			} else {
				response.getWriter().println(2);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void preedit(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String u_id = request.getParameter("data");
		System.out.println(u_id);
		try {
			User_Tb user = us.getById(Integer.valueOf(u_id));
			request.setAttribute("user", user);
		
			request.getRequestDispatcher("/page/user/user/user_edit.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void listAllByRoleSearchPagination(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int pageSize = Integer.valueOf(request.getParameter("pageSize"));
	/*	System.out.println("---------" + pageSize);*/
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		/*System.out.println(pageNum);*/
		String role = request.getParameter("role");
		/*System.out.println(role);*/
		String search = request.getParameter("search");
		/*System.out.println(search);*/
		try {
			// 表格数据===：
			List<User_Tb> userList = us.listAllByRoleSearchPagination(role, search, pageSize, pageNum);
			// 计算总页数：
			double pageCount = Math.ceil(us.getTotalBySearch(role, search) * 1.0 / pageSize);
			Map<String, Object> map = new HashMap<>();
			map.put("userList", userList);
			map.put("pageNum", pageNum);
			map.put("pageCount", pageCount);
			response.getWriter().print(new Gson().toJson(map));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void listAllByRolePagination(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int pageSize = Integer.valueOf(request.getParameter("pageSize"));
		/*System.out.println("==========" + pageSize);*/
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		/*System.out.println("========" + pageNum);*/
		String role = request.getParameter("role");
		System.out.println(role);

		try {
			// 表格数据===：
			List<User_Tb> userList = us.listAllByRolePagination(role, pageSize, pageNum);
			System.out.println(userList);
			// 分页条数据==:
			// 计算总页数：
			double pageCount = Math.ceil(us.getTotal(role) *1.0/pageSize);
			Map<String, Object> map = new HashMap<>();
			map.put("userList", userList);
			map.put("pageNum", pageNum);
			map.put("pageCount", pageCount);
			response.getWriter().print(new Gson().toJson(map));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void preAbb(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			List<Role_Tb> listAll = rs.listAll();
			System.out.println(listAll);
			request.setAttribute("listAll", listAll);
			request.getRequestDispatcher("/page/user/user/user_add.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void preAdd(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			List<Role_Tb> listAll = rs.listAll();
			request.setAttribute("listAll", listAll);
			request.getRequestDispatcher("/register.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String role = request.getParameter("role");
		/*System.out.println(role);*/
		String name = request.getParameter("name");
	/*	System.out.println(name);*/
		String head = request.getParameter("head");
		System.out.println(head);
		String pwd = request.getParameter("pwd");
		/*System.out.println(pwd);*/
		String email = request.getParameter("email");
		String telephone = request.getParameter("telephone");
		String brithday = request.getParameter("brithday");
		/*System.out.println(brithday);*/
		String gender = request.getParameter("gender");
		String address = request.getParameter("address");
		String question1 = request.getParameter("question1");
		String answer1 = request.getParameter("answer1");
		String question2 = request.getParameter("question2");
		String answer2 = request.getParameter("answer2");
		String remark = request.getParameter("remark");
		User_Tb ut = new User_Tb();
		ut.setHead(head);
		ut.setUser_name(name);
		ut.setPassword(pwd);
	
		ut.setEmail(email);
		ut.setTelephone(telephone);
		try {
			ut.setBrithday(new SimpleDateFormat("yyyy-MM-dd").parse(brithday));
			ut.setGender(gender);
			ut.setAddress(address);
			ut.setQuestion1(question1);
			ut.setAnswer1(answer1);
			ut.setQuestion2(question2);
			ut.setAnswer2(answer2);
			ut.setRemark(remark);
			ut.setRole(role);
			if (us.add(ut)) {
				response.getWriter().println(1);
			} else {
				response.getWriter().println(2);
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void deleteAll(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub]
		String u_ids = request.getParameter("u_ids");
		List<String> uids = new Gson().fromJson(u_ids, List.class);
		try {
			if (us.deleteAll(uids)) {
				response.getWriter().println(1);
			} else {
				response.getWriter().println(2);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void deleteById(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String user_id = request.getParameter("user_id");
		System.out.println(user_id);
		try {
			if (us.deleteById(Integer.valueOf(user_id))) {
				response.getWriter().println(1);
			} else {
				response.getWriter().println(2);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void Selectlist(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("utf-8");
		String role = request.getParameter("role");
		try {
			List<User_Tb> listrole = us.listByRole(role);
			/*System.out.println(listrole);*/
			Gson gson = new Gson();
			String json = gson.toJson(listrole);
		/*	System.out.println(json);*/
			response.getWriter().println(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void selectById(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("id");
		try {
			User_Tb byId = us.getById(Integer.valueOf(id));
			Gson gson = new Gson();
			String json = gson.toJson(byId);
			/*System.out.println(json);*/
			response.getWriter().println(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void login(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String role = request.getParameter("role");
		System.out.println("role");
		String name = request.getParameter("name");
		/*System.out.println(name);*/
		String password = request.getParameter("password");
		/*System.out.println(password);*/
		User_Tb user_tb = null;
		System.out.println("登录");
		try {
			if ((user_tb = us.selectmp(name, password)) != null) {
				if (("admin".equals(name))){
					request.getSession().setAttribute("user_tb", user_tb);
					response.sendRedirect(request.getContextPath() + "/main.jsp");
				} else {
					request.getSession().setAttribute("user_tb", user_tb);
					response.sendRedirect(request.getContextPath() + "/mainstorehouse.jsp");
				}
		
			} else {
				request.setAttribute("err", "密码错误");
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
